﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class PuOrderController :  MyControllerBase
    {
        PuOrderManager manager;
        PuOrderFetcher fetcher;
        PuOrderDetailFetcher dFetcher;
        public PuOrderController() : base()
        {
            this.manager = new PuOrderManager();
            this.fetcher = new PuOrderFetcher();
            this.dFetcher = new PuOrderDetailFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Supplier, AppOpt.Create)]
        public AsmMainDetail<PuOrderModel, PuOrderDetailModel> Set(dynamic dataJson)
        {
            string mainStr = (String)dataJson.main;
            string detailStr = (String)dataJson.detail;
            var main = Util.ConvertObjectFromJson<PuOrderModel>(mainStr);

            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{2}]{0}|[DETAIL]{1}", Util.ConvertToJson(main), dataJson, RequestIp));
            var details = Util.ConvertArrayFromJson<PuOrderDetailModel>(detailStr);
            if (main == null)
            {
                DBHelper.Commit(false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmMainDetail<PuOrderModel, PuOrderDetailModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            foreach (var item in details)
            {
                item.OrderId = main.AutoId;
            }
            var result = manager.Set(main, details);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit(result.Result);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Supplier, AppOpt.Delete)]
        public AsmObject<bool> Del(PuOrderModel model)
        {
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(model), RequestIp));
            if (model == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("DEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            AsmObject<bool> result = manager.Delete(model);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Approve)]
        public AsmObject<bool> Approve(PuOrderModel model)
        {
            LogHelper.Debug(this.GetType(), String.Format("APPROVE:[RQS|{1}]{0}", Util.ConvertToJson(model), RequestIp));
            if (model == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("APPROVE:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            AsmObject<bool> result = manager.Approve(model);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType(), String.Format("APPROVE:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.UnApprove)]
        public AsmObject<bool> UnApprove(PuOrderModel model)
        {
            LogHelper.Debug(this.GetType(), String.Format("UNAPPROVE:[RQS|{1}]{0}", Util.ConvertToJson(model), RequestIp));
            if (model == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("UNAPPROVE:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            AsmObject<bool> result = manager.UnApprove(model);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType(), String.Format("UNAPPROVE:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Supplier, AppOpt.Exec)]
        public AsmArray<PuOrderModel> List(Condition[] conditions, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", Util.ConvertToJson(conditions), RequestIp));
            int cnt = 0;
            var ls = fetcher.List(conditions, pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<PuOrderModel>() { Data = ls, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Supplier, AppOpt.Exec)]
        public AsmArray<PuOrderModel> ListBySupplier(int supplier, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYSUPPLIER:[RQS|{1}]{0}", Util.ConvertToJson(supplier), RequestIp));
            int cnt = 0;
            var ls = fetcher.ListBySupplier(supplier, pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<PuOrderModel>() { Data = ls, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYSUPPLIER:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Supplier, AppOpt.Exec)]
        public AsmMainDetail<PuOrderModel, PuOrderDetailModel> Get(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", Util.ConvertToJson(id), RequestIp));
            var main = fetcher.Get(id);
            if (main == null)
            {
                var errResponse = new AsmMainDetail<PuOrderModel, PuOrderDetailModel> { Data = main, Detail = null, Message = "找不到数据", Result = AmsCode.DataNotFound };
                DBHelper.Commit( false);
                LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            var detail = dFetcher.ListDetail(id);
            DBHelper.Commit( true);
            var result = new AsmMainDetail<PuOrderModel, PuOrderDetailModel> { Data = main, Detail = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }

        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Supplier, AppOpt.Exec)]
        public AsmArray<PuOrderDetailModel> ListDetail(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RQS|{1}]{0}", id, RequestIp));
            var detail = dFetcher.ListDetail(id);
            DBHelper.Commit(true);
            var result = new AsmArray<PuOrderDetailModel> { Data = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}